package com.example.sofa_module.sofa;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.viewbinding.ViewBinding;

import com.example.common.mvvm.view.BaseFragment;
import com.example.sofa_module.databinding.FragmentSofaBinding;
import com.example.sofa_module.sofa.fragments.pics.Fragment_child;
import com.example.sofa_module.sofa.fragments.text.Fragment_text;
import com.example.sofa_module.sofa.fragments.video.Fragment_video;

import java.util.ArrayList;
import java.util.List;

public class SofaFragment extends BaseFragment<FragmentSofaBinding,SofaViewModel> {

    List<Fragment>list_fragment=new ArrayList<>();
    List<String>list_string=new ArrayList<>();

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        FragmentSofaBinding fragmentSofaBinding=FragmentSofaBinding.inflate(inflater);
        return fragmentSofaBinding;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        vm.fragmentLiveDataBus.observe(this, new Observer<Fragment>() {
            @Override
            public void onChanged(Fragment fragment) {
                if (list_fragment.size()<3){
                    list_fragment.add(new Fragment_child());
                    list_fragment.add(new Fragment_video());
                    list_fragment.add(new Fragment_text());
                }
                if (list_string.size()<3){
                    list_string.add("图片");
                    list_string.add("视频");
                    list_string.add("文本");
                }
                if (list_string!=null&&list_fragment!=null){
                    MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), list_fragment, list_string);
                    v.vp.setAdapter(myFragmentPagerAdapter);
                    v.tab.setupWithViewPager(v.vp);
                }
            }
        });
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

        private List<Fragment>list_fragment;
        private List<String>list_string;

        public MyFragmentPagerAdapter(@NonNull FragmentManager fm, List<Fragment> list_fragment, List<String> list_string) {
            super(fm);
            this.list_fragment = list_fragment;
            this.list_string = list_string;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list_fragment.get(position);
        }

        @Override
        public int getCount() {
            return list_fragment.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return list_string.get(position);
        }
    }
}
